const axios = require('axios-https-proxy-fix')
const uas = [
  'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5',
  'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5',
  'Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5',
  'Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',
  'MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1',
  'Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10 Android Pad Moto Xoom',
  'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13',
  'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+',
  'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
  'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3',
  'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3',
  'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.65 Safari/535.11',
  'Mozilla/5.0 (iPhone 84; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.0 MQQBrowser/7.8.0 Mobile/14G60 Safari/8536.25 MttCustomUA/2 QBWebViewType/1 WKType/1',
  'Mozilla/5.0 (Linux; Android 7.0; STF-AL10 Build/HUAWEISTF-AL10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.49 Mobile MQQBrowser/6.2 TBS/043508 Safari/537.36 V1_AND_SQ_7.2.0_730_YYB_D QQ/7.2.0.3270 NetType/4G WebP/0.3.0 Pixel/1080',
  'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 MicroMessenger/6.5.18 NetType/WIFI Language/en',
  'Mozilla/5.0 (Linux; Android 5.1.1; vivo Xplay5A Build/LMY47V; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/48.0.2564.116 Mobile Safari/537.36 T7/9.3 baiduboxapp/9.3.0.10 (Baidu; P1 5.1.1)',
  'Mozilla/5.0 (Linux; U; Android 7.0; zh-cn; STF-AL00 Build/HUAWEISTF-AL00) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 Chrome/37.0.0.0 MQQBrowser/7.9 Mobile Safari/537.36',
  'Mozilla/5.0 (Linux; Android 6.0; LEX626 Build/HEXCNFN5902606111S) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/35.0.1916.138 Mobile Safari/537.36 T7/7.4 baiduboxapp/8.3.1 (Baidu; P1 6.0)',
  'Mozilla/5.0 (iPhone 92; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.0 MQQBrowser/7.7.2 Mobile/14F89 Safari/8536.25 MttCustomUA/2 QBWebViewType/1 WKType/1',
  'Mozilla/5.0 (Linux; U; Android 7.0; zh-CN; ZUK Z2121 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.6.8.952 Mobile Safari/537.36',
  'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Mobile/15A372 MicroMessenger/6.5.17 NetType/WIFI Language/zh_HK',
  'Mozilla/5.0 (Linux; U; Android 6.0.1; zh-CN; SM-C7000 Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.6.2.948 Mobile Safari/537.36',
  'MQQBrowser/5.3/Mozilla/5.0 (Linux; Android 6.0; TCL 580 Build/MRA58K; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/52.0.2743.98 Mobile Safari/537.36',
  'Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Mobile/14C92 MicroMessenger/6.5.16 NetType/WIFI Language/zh_CN',
  'Mozilla/5.0 (Linux; U; Android 5.1.1; zh-cn; MI 4S Build/LMY47V) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.146 Mobile Safari/537.36 XiaoMi/MiuiBrowser/9.1.3',
  'Mozilla/5.0 (Linux; U; Android 7.0; zh-CN; SM-G9550 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/40.0.2214.89 UCBrowser/11.7.0.953 Mobile Safari/537.36',
  'Mozilla/5.0 (Linux; Android 5.1; m3 note Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/48.0.2564.116 Mobile Safari/537.36 T7/9.3 baiduboxapp/9.3.0.10 (Baidu; P1 5.1)',
  'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Mobile Safari/537.36'
]

const Referer = 'http://juggletheball.xinwen.cn/worldCup'
const Host = 'z8.cnzz.com'
const ID = '1274127926'

const URL = `https://${Host}/stat.htm`

const http = axios.create({
  headers: {
    Host,
    Referer,
    Connection: 'keep-alive',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    Connection: 'keep-alive'
  }
})

http.interceptors.response.use(
  res => {
    return res
  },
  async err => {
    return Promise.reject(err)
  }
)

let step = 13000
let proxies = []
let success_count = 1
let failed_count = 1

const umuuid = (function() {
  function a(UA) {
    function a(a, b) {
      var c,
        d = 0
      for (c = 0; c < b.length; c++) d |= h[c] << (8 * c)
      return a ^ d
    }
    var b = UA,
      f,
      g,
      h = [],
      k = 0
    for (f = 0; f < b.length; f++) {
      ;(g = b.charCodeAt(f)), h.unshift(g & 255), h.length >= 4 && ((k = a(k, h)), (h = []))
    }
    h.length > 0 && (k = a(k, h))
    return k.toString(16)
  }
  function b() {
    for (var a = 1 * new Date(), b = 0; a == 1 * new Date(); ) b++
    return a.toString(16) + b.toString(16)
  }
  return function(UA) {
    var c = (640 * 360).toString(16)
    return (
      b() +
      '-' +
      Math.random()
        .toString(16)
        .replace('.', '') +
      '-' +
      a(UA) +
      '-' +
      c +
      '-' +
      b()
    )
  }
})()

async function brush() {
  const UA = uas[~~(Math.random() * uas.length)]

  let params = {
    id: ID,
    r: '',
    lg: 'zh-cn',
    ntime: Math.floor(new Date().getTime() / 1000).toString(),
    cnzz_eid: `${Math.floor(2147483648 * Math.random())}-${Math.floor(
      new Date().getTime() / 1000 - ~~Math.random() * 85978
    )}-`,
    showp: '360x640',
    t: UA,
    umuuid: umuuid(UA),
    h: '1',
    rnd: Math.floor(2147483648 * Math.random()).toString()
  }

  fetch(params, UA)
  fetch(params, UA)
  fetch(params, UA)
}

async function fetch(params, UA) {
  if (!proxies.length) return
  // console.log('​Proxies left : ', proxies.length)

  const random = ~~(Math.random() * proxies.length)
  const currentProxies = proxies[random]
  proxy = currentProxies.split(':')

  return http(URL, {
    headers: {
      'User-Agent': UA
    },
    params,
    proxy: {
      host: proxy[0],
      port: +proxy[1]
    }
  })
    .then(res => {
      console.log('--------[ Success ]------', success_count, res.data)
      success_count++
      delProxy(currentProxies)
      return res
    })
    .catch(async _ => {
      console.log('😡 Bad proxy: ', failed_count)
      failed_count++
      delProxy(currentProxies)
      fetch()
    })
}

function delProxy(proxy) {
  const index = proxies.findIndex(e => e === proxy)
  proxies.splice(index, 1)
}

async function getProxyZdaye() {
  await axios.get('http://47.92.84.187:8088/proxy/ip?count=5').then(res => {
    data = res.data.data.map(e => e.ip + ':' + e.port)
    console.log('----> Fetch proxies successful <-----')
    proxies = proxies.concat(data)
  })

  global.zdayeSchedule = setTimeout(async _ => {
    getProxyZdaye()
  }, 11000)
}

async function start() {
  global.zdayeSchedule = setTimeout(async _ => {
    getProxyZdaye()
  }, 0)

  global.timer = setInterval(async _ => {
    if (success_count > step) {
      clearInterval(global.timer)
      return
    }
    await brush()
  }, 2000)
}

start()

module.exports = start
